package ZooManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
* User: James Barker
* Date: 16/08/12
*/
public class CageViewUI extends JDialog {
    Cage cage;
    ArrayList<JLabel> doorStateLabelArray = new ArrayList<JLabel>();
    ArrayList<JButton> toggleDoorStateButtonArray = new ArrayList<JButton>();
    JLabel cageOccupiedLabel;
    JButton toggleCageOccupiedButton;

    /**
     * View Cage UI Constructor
     */
    public CageViewUI(Cage cage) {
        this.cage = cage;

        createCageOccupiedPanel();
        createCageDetailPanel();
        createCageEntryPanel();
        createCageLightPanel();

        this.setTitle("Cage Detail");
        this.setSize(540, 400);
        this.setLocationRelativeTo(this);
        this.setModal(true);
        this.setVisible(true);
        this.setResizable(false);
        this.pack();
    }


    public void createCageDetailPanel() {
        JPanel cageDetailPanel = new JPanel(new GridLayout(8, 2));
        cageDetailPanel.setBorder(BorderFactory.createTitledBorder("Cage Information"));
        cageDetailPanel.add(new JLabel("Enclosure Name:"));
        cageDetailPanel.add(new JLabel(cage.getEnclosureName()));
        cageDetailPanel.add(new JLabel("Enclosure Number:"));
        cageDetailPanel.add(new JLabel(Integer.toString(cage.getEnclosureNumber())));
        cageDetailPanel.add(new JLabel("Enclosure Size:"));
        cageDetailPanel.add(new JLabel(Integer.toString(cage.getEnclosureSize())));
        cageDetailPanel.add(new JLabel("Enclosure Type:"));
        cageDetailPanel.add(new JLabel(cage.getEnclosureType()));
        cageDetailPanel.add(new JLabel("Number of Entries:"));
        cageDetailPanel.add(new JLabel(Integer.toString(cage.getNoOfEntries())));
        cageDetailPanel.add(new JLabel("Exhibit Name:"));
        cageDetailPanel.add(new JLabel(cage.getExhibitName()));
        cageDetailPanel.add(new JLabel("Exhibit Description:"));
        cageDetailPanel.add(new JLabel(cage.getExhibitDesc()));
        this.add(cageDetailPanel, BorderLayout.WEST);
    }


    public void createCageEntryPanel() {
        final ArrayList<Entry> tt = cage.getEntries();
        final JPanel cageEntryPanel = new JPanel();
        cageEntryPanel.setLayout(new BoxLayout(cageEntryPanel, BoxLayout.PAGE_AXIS));
        cageEntryPanel.setSize(300, 300);
        cageEntryPanel.setBorder(BorderFactory.createTitledBorder("Entries"));
        for (final Entry entry : tt) {
            JPanel entryPanel = new JPanel();
            entryPanel.setBorder(BorderFactory.createTitledBorder("Entry " + (tt.indexOf(entry) + 1) + " - " + entry.getEntryType()));
            final JLabel doorStateLabel = new JLabel();
            doorStateLabelArray.add(doorStateLabel);
            final JButton toggleDoorStateButton = new JButton();
            toggleDoorStateButtonArray.add(toggleDoorStateButton);
            if (entry.isEntryOpen()) {
                doorStateLabel.setText("Entry is Open");
                doorStateLabel.setForeground(Color.RED);
                toggleDoorStateButton.setText("Close Entry");
            } else {
                doorStateLabel.setText("Entry is Closed");
                doorStateLabel.setForeground(Color.BLACK);
                toggleDoorStateButton.setText("Open Entry");
            }
            toggleDoorStateButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (entry.isEntryOpen()) {
                        entry.closeEntry();
                        doorStateLabel.setText("Entry is Closed");
                        doorStateLabel.setForeground(Color.BLACK);
                        toggleDoorStateButton.setText("Open Entry");
                    } else {
                        entry.openEntry();
                        doorStateLabel.setText("Entry is Open");
                        doorStateLabel.setForeground(Color.RED);
                        toggleDoorStateButton.setText("Close Entry");
                    }
                }
            });
            entryPanel.add(doorStateLabel);
            entryPanel.add(toggleDoorStateButton);
            cageEntryPanel.add(entryPanel);
        }
        JPanel secureEntryButtonPanel = new JPanel();
        JButton secureEntryButton = new JButton("Secure Cage");
        secureEntryButton.setSize(300, 50);
        secureEntryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cage.closeAllEntries();
                for (int i = 0; i < doorStateLabelArray.size(); i++) {
                    doorStateLabelArray.get(i).setText("Entry is Closed");
                    doorStateLabelArray.get(i).setForeground(Color.BLACK);
                    toggleDoorStateButtonArray.get(i).setText("Open Entry");
                }
                cage.setCageUnoccupied();
                cageOccupiedLabel.setText("Cage is Unoccupied");
                cageOccupiedLabel.setForeground(Color.BLACK);
                toggleCageOccupiedButton.setText("Occupy");
            }
        });
        secureEntryButtonPanel.add(secureEntryButton);
        cageEntryPanel.add(secureEntryButtonPanel);
        this.add(cageEntryPanel, BorderLayout.CENTER);
    }


    public void createCageLightPanel() {
        final JPanel cageLightPanel = new JPanel();
        cageLightPanel.setBorder(BorderFactory.createTitledBorder("Light Status"));
        final JLabel lightStateLabel = new JLabel();
        final JButton toggleLightStateButton = new JButton("Lights");

        if (cage.isLightsOn()) {
            lightStateLabel.setText("Lights are On");
            lightStateLabel.setForeground(Color.GREEN);
        } else {
            lightStateLabel.setText("Lights are Off");
            lightStateLabel.setForeground(Color.BLACK);
        }

        toggleLightStateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (cage.isLightsOn()) {
                    cage.turnLightsOff();
                    lightStateLabel.setText("Lights are Off");
                    lightStateLabel.setForeground(Color.BLACK);
                } else {
                    cage.turnLightsOn();
                    lightStateLabel.setText("Lights are On");
                    lightStateLabel.setForeground(Color.GREEN);
                }
            }
        });
        cageLightPanel.add(lightStateLabel);
        cageLightPanel.add(toggleLightStateButton);
        this.add(cageLightPanel, BorderLayout.SOUTH);
    }


    public void createCageOccupiedPanel() {
        final JPanel cageOccupiedPanel = new JPanel();
        cageOccupiedPanel.setBorder(BorderFactory.createTitledBorder("Occupied Status"));
        cageOccupiedLabel = new JLabel();
        toggleCageOccupiedButton = new JButton();

        if (cage.isCageOccupied()) {
            cageOccupiedLabel.setText("Cage is Occupied");
            cageOccupiedLabel.setForeground(Color.RED);
            toggleCageOccupiedButton.setText("Unoccupy");
        } else {
            cageOccupiedLabel.setText("Cage is Unoccupied");
            cageOccupiedLabel.setForeground(Color.BLACK);
            toggleCageOccupiedButton.setText("Occupy");
        }

        toggleCageOccupiedButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (cage.isCageOccupied()) {
                    cage.setCageUnoccupied();
                    cageOccupiedLabel.setText("Cage is Unoccupied");
                    cageOccupiedLabel.setForeground(Color.BLACK);
                    toggleCageOccupiedButton.setText("Occupy");
                } else {
                    cage.setCageOccupied();
                    cageOccupiedLabel.setText("Cage is Occupied");
                    cageOccupiedLabel.setForeground(Color.RED);
                    toggleCageOccupiedButton.setText("Unoccupy");
                }
            }
        });
        cageOccupiedPanel.add(cageOccupiedLabel);
        cageOccupiedPanel.add(toggleCageOccupiedButton);
        this.add(cageOccupiedPanel, BorderLayout.NORTH);
    }
}
